//
//  LBEmptyDataBusinessView.m
//  LBEmptyData
//
//  Created by 杨庆人 on 2017/9/29.
//

#import "LBEmptyDataBusinessView.h"
#import <Masonry/Masonry.h>
#import <ReactiveObjC/ReactiveObjC.h>

@interface LBEmptyDataBusinessView ()
@property (nonatomic,strong)UIImageView *imageView;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UILabel *detailLabel;
@end
@implementation LBEmptyDataBusinessView

- (instancetype)initWithBsImage:(UIImage *)image title:(NSString *)title detail:(NSString *)detail button:(NSArray <NSString *>*)button block:(LBEmptyDataIndexBlock)block {
    if (self = [super init]) {
        
        self.showsVerticalScrollIndicator = NO;
        
        [self addSubview:self.imageView];
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self).offset(70);
            make.centerX.mas_equalTo(self.mas_centerX);
            make.size.mas_equalTo(CGSizeMake(200, 150));
        }];
        
        [self addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(20);
            make.right.offset(-20);
            make.top.mas_equalTo(self.imageView.mas_bottom).offset(40);
            make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width - 20 * 2);
        }];

        [self addSubview:self.detailLabel];
        [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(20);
            make.right.offset(-20);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10);
        }];

        __weak typeof(self)weakSelf = self;
        [button enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton *subButton = [weakSelf businessButton];
            [subButton setTitle:obj forState:UIControlStateNormal];
            [weakSelf addSubview:subButton];

            [subButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.offset(-95);
                make.left.offset(95);
                make.top.mas_equalTo(self.detailLabel.mas_bottom).offset(56 + (20 +44) * idx);
                make.height.mas_equalTo(44);
                if (idx == button.count - 1) {
                    make.bottom.offset(-30);
                }
            }];
            
            [[subButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
                !block ? :block(idx);
            }];
        }];

        self.imageView.image = image;
        self.titleLabel.text = title;
        self.detailLabel.text = detail;
    }
    return self;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _imageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:28];
        _titleLabel.textColor = [UIColor colorWithRed:56/255.0 green:140/255.0 blue:219/255.0 alpha:1];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UILabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.font = [UIFont systemFontOfSize:18];
        _detailLabel.numberOfLines = 0;
        _detailLabel.textAlignment = NSTextAlignmentCenter;
        _detailLabel.textColor = [UIColor colorWithRed:56/255.0 green:140/255.0 blue:219/255.0 alpha:1];
    }
    return _detailLabel;
}

- (UIButton *)businessButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"LBEmptyData.bundle/EmptyResGradientBtn"] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:18];
    button.layer.cornerRadius = 21;
    button.clipsToBounds = YES;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    return button;
}


@end
